<?php

namespace model;

use common\Model;

class ondutys extends Model
{
    const TABLE = 'onduty';
    private $DB;
    function __construct()
    {
        $this->DB = parent::db();
    }
    public function findData()
    {
        return $this->DB->table(self::TABLE)->where(['id' => $_GET['id']])->find();
    }
    //查询列表
    public function selectData($limit)
    {
        $where['status'] = 1;
        return $this->DB->table(self::TABLE)->where($where)->limit($limit)->cache(600)->Order('id DESC')->select();
    }

    //获取数据并分页
    public function jsonPageSelect()
    {
        $p = intval($_GET['page'] ?? 0) ?: 1;
        $num = intval($_GET['limit'] ?? 0) ?: 15;
        $page = ['num' => $num, 'p' => $p, 'return' => true];
        return [
            'data' => $this->DB->table(self::TABLE)->Page($page)->order('id DESC')->select(),
            'page' => $this->DB->getPage()
        ];
    }

    //添加数据
    public function insertDataBatch()
    {
        if (empty($_POST['content']))
            return ['status' => 0, 'msg' => '请填写批量内容'];
        $text = $_POST['content'];
        $order = array("\r\n", "\n", "\r");
        $arr = str_replace($order, "&", trim($text)); //将各样的行符转成&
        $arr = explode("&", $arr);
        $arr = preg_replace("/\s(?=\s)/", "\\1",  $arr); //将多个空格转成一个空格
        $data = [];
        foreach ($arr as $vo) {
            $newarr = explode(" ", $vo);
            $pusharr = [
                $newarr[0],
                $newarr[1],
                $newarr[2],
                $newarr[3],
                $newarr[4],
                1
            ];
            array_push($data, $pusharr);
        }
        $keys = ['juname', 'chuname', 'yuanname', 'phone', 'ondutytime', 'status'];
        $re_key = $this->DB->table(self::TABLE)->BatchInsert($keys, $data);
        if ($re_key > 0) {
            //写入日志
            SqlLog("批量添加值班 {$re_key} 条");
            CleanDbCache(self::TABLE);
            return ['status' => 1, 'msg' => '添加成功'];
        } else {
            return ['status' => 0, 'msg' => '数据错误！添加失败'];
        }
    }
    //添加数据
    public function insertData()
    {
        if (empty($_POST['juname']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $data = [
            'juname' => VarFilter($_POST['juname']),
            'chuname' => VarFilter($_POST['chuname']),
            'yuanname' => VarFilter($_POST['yuanname']),
            'phone' => VarFilter($_POST['phone']),
            'ondutytime' => VarFilter($_POST['ondutytime']),
            'status' => $_POST['status'],
        ];
        $re_key = $this->DB->table(self::TABLE)->insert($data);
        if ($re_key) {
            //写入日志
            SqlLog("添加值班 ID：" . $re_key);
            CleanDbCache(self::TABLE);
            return ['status' => 1, 'msg' => '添加成功'];
        } else {
            return ['status' => 0, 'msg' => '数据错误！添加失败'];
        }
    }

    //更新数据
    public function updateData()
    {
        if (empty($_POST['juname']))
            return  ['status' => 0, 'msg' => '请填写必要数据'];
        $where['id'] = $_POST['id'];
        $data = [
            'juname' => VarFilter($_POST['juname']),
            'chuname' => VarFilter($_POST['chuname']),
            'yuanname' => VarFilter($_POST['yuanname']),
            'phone' => VarFilter($_POST['phone']),
            'ondutytime' => VarFilter($_POST['ondutytime']),
            'status' => $_POST['status'],
        ];
        $re_key = $this->DB->table(self::TABLE)->Where($where)->Update($data);
        if ($re_key) {
            //写入日志
            SqlLog("修改值班 ID：" . $_POST['id']);
            CleanDbCache(self::TABLE);
            return ['status' => 1, 'msg' => '修改成功'];
        } else {
            return ['status' => 0, 'msg' => '数据没有变化'];
        }
    }

    //删除一条数据
    public function deleteOneData()
    {
        $where['id'] = $_POST['id'];
        $result = $this->DB->table(self::TABLE)->where($where)->Delete();
        //写入日志
        SqlLog("删除值班 ID：" . $_POST['id']);

        return $result;
    }

    //设置状态
    public function switchStatus()
    {
        $where['id'] = $_POST['id'];
        $data = [
            'status' => $_POST['status']
        ];
        $result = $this->DB->table(self::TABLE)->where($where)->Update($data);
        //写入日志
        SqlLog("切换值班状态 ID：" . $_POST['id']);

        return $result;
    }
}
